
#include "BUtils.h"

using namespace blueberry;

unsigned long UTLHash::RSHash(char * _zKey)
{
	unsigned long b = 378551;
	unsigned long a = 63689;
	unsigned long hash = 0;

	while(*_zKey)
	{
		hash = hash * a + (*_zKey++);
		a = a * b;
	}

	return (hash & 0x7FFFFFFF);
}
unsigned long UTLHash::JSHash(char * _zKey)
{
	unsigned long hash = 1315423911;

	while(*_zKey)
	{
		hash ^= ((hash << 5) + (*_zKey++) + (hash >> 2));
	}

	return (hash & 0x7FFFFFFF);
}
unsigned long UTLHash::PJWHash(char * _zKey)
{
	const unsigned int BitsInUnsignedInt = (unsigned int) (sizeof(unsigned int) * 8);
	const unsigned int ThreeQuarters = (unsigned int) ((BitsInUnsignedInt * 3) / 4);
	const unsigned int OneEighth = (unsigned int) (BitsInUnsignedInt / 8);
	const unsigned int HighBits = (unsigned int) (0xFFFFFFFF) << (BitsInUnsignedInt - OneEighth);
	unsigned int hash = 0;
	unsigned int test = 0;

	while(*_zKey)
	{
		hash = (hash << OneEighth) + (*_zKey++);

		if ((test = hash & HighBits) != 0)
		{
			hash = ((hash ^ (test >> ThreeQuarters)) & (~HighBits));
		}
	}

	return hash;
}
unsigned long UTLHash::ELFHash(char * _zKey)
{
	unsigned int hash = 0;
	unsigned int x = 0;

	while(*_zKey)
	{
		hash = (hash << 4) + (*_zKey++);
		if ((x = hash & 0xF0000000L) != 0)
		{
			hash ^= (x >> 24);
		}
		hash &= ~x;
	}

	return hash;
}
unsigned long UTLHash::BKDRHash(char * _zKey)
{
	unsigned int seed = 131; /* 31 131 1313 13131 131313 etc.. */
	unsigned int hash = 0;

	while(*_zKey)
	{
		hash = (hash * seed) + (*_zKey++);
	}

	return hash;
}
unsigned long UTLHash::SDBMHash(char * _zKey)//GOOD
{
	unsigned long hash = 0;

	while(*_zKey)
	{
		hash = (*_zKey++) + (hash << 6) + (hash << 16) - hash;
	}

	return hash;
}
unsigned long UTLHash::DJBHash(char * _zKey)//GOOD
{
	unsigned int hash = 5381;

	while(*_zKey)
	{
		hash = ((hash << 5) + hash) + (*_zKey++);
	}

	return hash;
}
unsigned long UTLHash::DEKHash(char * _zKey)
{
	unsigned int hash = strlen(_zKey);

	while(*_zKey)
	{
		hash = ((hash << 5) ^ (hash >> 27)) ^ (*_zKey++);
	}
	return hash;
}
unsigned long UTLHash::BPHash(char * _zKey)
{
	unsigned int hash = 0;

	while(*_zKey)
	{
		hash = hash << 7 ^ (*_zKey++);
	}

	return hash;
}
unsigned long UTLHash::FNVHash(char * _zKey)
{
	const unsigned long fnv_prime = 0x811C9DC5;
	unsigned long hash = 0;

	while(*_zKey)
	{
		hash *= fnv_prime;
		hash ^= (*_zKey++);
	}

	return hash;
}
unsigned long UTLHash::APHash(char * _zKey)
{
	unsigned long hash = 0xAAAAAAAA;
	int i = 0;

	while(*_zKey)
	{
		hash ^= ((i & 1) == 0) ? ((hash << 7) ^ (*_zKey) * (hash >> 3)) : (~((hash << 11) + ((*_zKey) ^ (hash >> 5))));
		i++;
		_zKey++;
	}

	return hash;
}


